<?php

namespace frontend\modules\marketing\models;

use common\models\Wxuser;
use Yii;

/**
 * This is the model class for table "coupon_user".
 *
 * @property string $coupon_user_id
 * @property string $user_id
 * @property string $coupon_id
 * @property integer $is_used
 * @property integer $number
 * @property integer $created_at
 *
 * @property User $user
 */
class CouponUser extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'coupon_user';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
//            TimestampBehavior::className(),
            // 名为myBehavior4的行为，也是给出了MyBehavior类的配置数组
            'uuid' => [
                'class' => \common\behavior\uuidBehavior::className(),
                'owner' => $this
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['coupon_user_id','default','value'=>$this->getUUID()],
            ['is_used','default','value'=>0],
            ['created_at','default','value'=>time()],
            [['coupon_user_id', 'user_id', 'coupon_id'], 'required'],
            [['is_used', 'number', 'created_at'], 'integer'],
            [['coupon_user_id', 'user_id', 'coupon_id'], 'string', 'max' => 40],
            [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => Wxuser::className(), 'targetAttribute' => ['user_id' => 'id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'coupon_user_id' => Yii::t('app', 'Coupon User ID'),
            'user_id' => Yii::t('app', 'User ID'),
            'coupon_id' => Yii::t('app', 'Coupon ID'),
            'is_used' => Yii::t('app', 'Is Used'),
            'number' => Yii::t('app', 'Number'),
            'created_at' => Yii::t('app', 'Created At'),
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCoupon()
    {
        return $this->hasOne(Coupon::className(), ['coupon_id' => 'coupon_id']);
    }

    /**
     * @inheritdoc
     * @return \frontend\modules\marketing\models\query\CouponUserQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new \frontend\modules\marketing\models\query\CouponUserQuery(get_called_class());
    }
}
